import { ApiOkResponse, ApiOperation, ApiResponse, ApiSecurity, ApiTags } from '@nestjs/swagger';
import { Controller, Get, Inject, Query, Post, Body } from '@nestjs/common';

import { ADMIN_PREFIX } from '../../admin.constants';
import { PermissionOptional } from '../../core/decorators/permission-optional.decorator';
import { Authorize } from '../../core/decorators/authorize.decorator';
import { ServeStatInfo, DrawImageParams } from './serve.class';
import { SysServeService } from './serve.service';
import { ClientProxy } from '@nestjs/microservices';
import { Observable } from 'rxjs';
@ApiSecurity(ADMIN_PREFIX)
@ApiTags('服务监控')
@Controller('serve')
export class SysServeController {
  constructor(
    private serveService: SysServeService,
    @Inject('CANVAS_SERVICE') private canvasClient: ClientProxy,
  ) {}

  @ApiOperation({ summary: '获取服务器运行信息' })
  @ApiOkResponse({ type: ServeStatInfo })
  @ApiResponse({ status: 200, description: 'stat' })
  @PermissionOptional()
  @Get('stat')
  async stat(): Promise<ServeStatInfo> {
    return await this.serveService.getServeStat();
  }

  @ApiOperation({ summary: 'canvas微服务测试' })
  @PermissionOptional()
  @Get('canvas')
  canvas(@Query('num') str): Observable<number> {
    const numArr = str.split(',').map((item) => parseInt(item));
    return this.canvasClient.send('sum', numArr);
  }

  @ApiOperation({ summary: 'canvas图像测试' })
  @PermissionOptional()
  @Get('canvas-test')
  canvasTest(): Observable<string> {
    return this.canvasClient.send('canvas', 123444);
  }

  @ApiOperation({ summary: 'canvas生成图像测试' })
  @Authorize() // 无需权限限制可以使用这个
  // @PermissionOptional() // 无需鉴权但需要token验证使用这个
  @Post('genImage')
  async genImage(@Body() dto: DrawImageParams[]): Promise<Observable<void>> {
    return await this.canvasClient.send('genImage', dto);
  }
}
